package databases

import (
	"errors"
	"github.com/go-xorm/xorm"
	"gitlab.gaodun.com/golib/gderror"
	"permission/config"
	"permission/vigo_loger"
)

type DepartUserFactory struct {
	Table        Tpo_Sys_Department_User
	engine       *xorm.Engine
	RowsSlicePtr []Tpo_Sys_Department_User
}

// 返回 user factory
func NewDepartUserFactory() *DepartUserFactory {
	funcFac := DepartUserFactory{}
	funcFac.engine, _ = config.InitEng()
	return &funcFac
}

// 根据用户 id 获取数据
func (duf *DepartUserFactory) FindInfoByUserId(userId int64) error {
	if userId == 0 {
		return errors.New("userId is null")
	}
	err := duf.engine.Where(" UserId = ? ", userId).Find(&(duf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}

	return err
}

func (duf *DepartUserFactory) FindInfoByDepartId(departId int64) error {
	if departId == 0 {
		return errors.New("departId is null")
	}
	err := duf.engine.Where(" UserId = ? ", departId).Find(&(duf.RowsSlicePtr))
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}

	return err
}

func (duf *DepartUserFactory) EdsAddRelation() (int64, error) {
	return duf.engine.InsertOne(&(duf.Table))
}